package com.zzh.takeoutapp.view;

import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.adapter.DiscountHeaderBannerAdapter;
import com.zzh.takeoutapp.utils.DensityUtil;
import com.zzh.takeoutapp.utils.ImageManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/25.
 */

public class DiscountHeaderBannerView extends AbsHeaderView<List<String>> {

    @Bind(R.id.vp_banner)
    ViewPager vpBanner;
    @Bind(R.id.rl_banner)
    RelativeLayout rlBanner;
    private List<ImageView> ivList;
    private int bannerHeight;
    private Context mContext;
    private int bannerCount;

    public DiscountHeaderBannerView(Activity activity) {
        super(activity);
        this.mContext = activity;
        ivList = new ArrayList<>();
        bannerHeight = DensityUtil.getWindowWidth(activity) * 9 / 16;
    }

    @Override
    public View getView(List<String> strings) {
        View listView = mInflate.inflate(R.layout.header_banner_discount_layout, null);
        ButterKnife.bind(this, listView);
        dealWithTheView(strings);
        return listView;
    }

    // 创建要显示的ImageView
    private void createImageViews(List<String> list) {
        for (int i = 0; i < list.size(); i++) {
            ImageView imageView = new ImageView(mActivity);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            imageView.setLayoutParams(params);
            ImageManager.loadUrlImage(mActivity, list.get(i), imageView);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ivList.add(imageView);
        }
    }

    private void dealWithTheView(List<String> list) {
        ivList.clear();
        bannerCount = list.size();
        if (bannerCount == 2) {
            list.addAll(list);
        }
        RelativeLayout.LayoutParams layoutParams =
                new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.height = bannerHeight;
        rlBanner.setLayoutParams(layoutParams);
        createImageViews(list);
        DiscountHeaderBannerAdapter adapter = new DiscountHeaderBannerAdapter(ivList, mContext);
        vpBanner.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}
